<?php

namespace App\Http\Controllers;



use App\Http\Requests;
use Illuminate\Support\Facades\Request;
use ServiceClass\Admin;
use Illuminate\Support\Facades\DB;
use Validator;

class AdminController extends Controller
{
    //
    public function __construct(Admin $admin){

        $this->admin = $admin;
    }

    public function login(){

        $input = Request::only('admin_name', 'admin_password');

        $validator = Validator::make($input, [
            'admin_name' => 'required|max:16|min:4',
            'admin_password' => 'required|max:16|min:4',
        ]);
        if ($validator->fails()) {
            return $validator->messages();
        }

        $where['admin_name'] = $input['admin_name'];
        $where['admin_password'] = md5($input['admin_password']);

       // dump($where);
        $result = DB::table('admin')->where($where)->first();
        if($result){

            $token = md5(date('Y-m-d H:i:s').'.'.rand(0,10000));

            DB::table('admin')->where('admin_name',$input['admin_name'])->update([
                'admin_token' => $token
            ]);

            $re = array($input['admin_name'],$token);
            return json_encode($re);
        }else{

            return json_encode(['msg'=>0]);
        }
    }

    public function addAdmin(){


        $input = Request::only('admin_name', 'admin_password');

        $validator = Validator::make($input, [
            'admin_name' => 'required|max:16|min:4',
            'admin_password' => 'required|max:16|min:4',
        ]);
        if ($validator->fails()) {
            return $validator->messages();
        }

        $result = $this->admin->addAdmin($input);

        if($result == 1){

            return json_encode(['msg'=>'添加管理员成功']);
        }else{

            return json_encode($result);
        }


    }

    public function deleteAdmin(){

        $admin_id   = Request::only('admin_id');

        $result = $this->admin->deleteAdmin($admin_id);
        if($result == 1){

            return json_encode(['msg'=>'删除成功']);
        }else{

            return json_encode(['msg'=>"服务器错误"]);
        }
    }

    public function searchAdmin(){

        $admin_id   = Request::only('admin_id');

        $result = $this->admin->searchAdmin($admin_id);
        if($result == 0){


            return json_encode(['msg'=>"服务器错误"]);
        }else{


            return json_encode($result);
        }
    }

    public function updateAdmin(){


        $input = Request::only('admin_password','admin_id');

        $validator = Validator::make($input, [
            'admin_password' => 'required|max:16|min:4',
        ]);
        if ($validator->fails()) {
            return $validator->messages();
        }

        $result = $this->admin->updateAdmin($input);

        if($result == 1){

            return json_encode(['msg'=>'修改管理员成功']);
        }else{


            return json_encode($result);
        }
    }

    public function selectAdminList(){

        $data = Request::all();
        $page = $data['page'];

        $result = $this->admin->selectAdminList('admin','10','',$page);


        return json_encode($result);
    }
}
